package com.cn.serverline.lineicp.workshopPlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.serverline.lineicp.workshopPlan.entity.SparePartsType;
import com.cn.serverline.lineicp.workshopPlan.entity.TypesOfConsumables;
import com.cn.serverline.lineicp.workshopPlan.entity.request.TypesOfConsumablesRequest;
import com.cn.serverline.lineicp.workshopPlan.enums.RolingPlanException;
import com.cn.serverline.lineicp.workshopPlan.mapper.SparePartsTypeMapper;
import com.cn.serverline.lineicp.workshopPlan.mapper.TypesOfConsumablesMapper;
import com.cn.serverline.lineicp.workshopPlan.service.TypesOfConsumablesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 *
 * 消耗材料服务实现
 */
@Service
public class TypesOfConsumablesServiceImpl extends ServiceImpl<TypesOfConsumablesMapper, TypesOfConsumables> implements TypesOfConsumablesService {

    @Resource
    private TypesOfConsumablesMapper typesOfConsumablesMapper;
    /**
     * 添加消耗材料
     * @param typesOfConsumablesRequest 请求参数
     * @return
     */
    @Override
    public Long add(TypesOfConsumablesRequest typesOfConsumablesRequest) {

        //判断编号是否重复
        LambdaQueryWrapper<TypesOfConsumables> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TypesOfConsumables::getCode, typesOfConsumablesRequest.getCode());
        TypesOfConsumables existingSparePart = typesOfConsumablesMapper.selectOne(queryWrapper);
        if (existingSparePart != null) {
            // 如果编号已存在，抛出异常或返回错误信息
            throw new ServiceException(RolingPlanException.ONE);
        }

        TypesOfConsumables spareParts = new TypesOfConsumables();
        BeanUtil.copyProperties(typesOfConsumablesRequest, spareParts);
        spareParts.setCreateTime(new Date());
        this.save(spareParts);
        //返回新创建的SparePartsType实例的id，作为一个字符串。
        return spareParts.getId();
    }

    /**
     *删除消耗材料
     * @param typesOfConsumablesRequest 请求参数
     */
    @Override
    public void del(TypesOfConsumablesRequest typesOfConsumablesRequest) {
        this.removeById(typesOfConsumablesRequest.getId());
    }

    /**
     * 修改消耗材料
     * @param typesOfConsumablesRequest 请求参数
     */
    @Override
    public void edit(TypesOfConsumablesRequest typesOfConsumablesRequest) {

        //判断编号是否重复
        LambdaQueryWrapper<TypesOfConsumables> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TypesOfConsumables::getCode, typesOfConsumablesRequest.getCode());
        TypesOfConsumables existingSparePart = typesOfConsumablesMapper.selectOne(queryWrapper);
        if (existingSparePart != null) {
            // 如果编号已存在，抛出异常或返回错误信息
            throw new ServiceException(RolingPlanException.ONE);
        }
        // 首先，我们需要根据请求中的ID找到要修改的零组件记录
        TypesOfConsumables auxiliaryMaterials = this.getById(typesOfConsumablesRequest.getId());

        // 如果记录不存在，则抛出异常或返回错误信息
        if (auxiliaryMaterials == null) {
            throw new ServiceException(RolingPlanException.ZERO);
        }

        // 使用BeanUtil.copyProperties方法将请求参数中的属性复制到找到的零组件记录中
        BeanUtil.copyProperties(typesOfConsumablesRequest, auxiliaryMaterials);

        // 更新修改时间
        auxiliaryMaterials.setUpdateTime(new Date());

        // 调用save方法来持久化更改
        this.updateById(auxiliaryMaterials);
    }

    /**
     * 查询消耗材料
     * @param typesOfConsumablesRequest 请求参数
     * @return 匹配的零组件列表
     */
    @Override
    public Page<TypesOfConsumables> findList(TypesOfConsumablesRequest typesOfConsumablesRequest) {
        // 创建查询条件构建器
        LambdaQueryWrapper<TypesOfConsumables> queryWrapper = new LambdaQueryWrapper<>();

        // 根据请求参数构建查询条件
        queryWrapper.like(typesOfConsumablesRequest.getName() != null, TypesOfConsumables::getName, typesOfConsumablesRequest.getName());
        queryWrapper.eq(typesOfConsumablesRequest.getId() != null, TypesOfConsumables::getId, typesOfConsumablesRequest.getId());

        // 返回查询结果
        return this.page(PageFactory.defaultPage(), queryWrapper);
    }
}
